package com.hjq.demo.ui.fragment;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.Button;

import com.hjq.base.BaseDialog;
import com.hjq.demo.R;
import com.hjq.demo.aop.SingleClick;
import com.hjq.demo.app.TitleBarFragment;
import com.hjq.demo.ui.activity.HomeActivity;
import com.hjq.demo.ui.adapter.TabAdapter;
import com.hjq.demo.ui.dialog.WaitDialog;
import com.hjq.demo.ui.helpers.MessageEvent;
import com.hjq.demo.ui.helpers.MySocketClientManager;
import com.hjq.demo.widget.XCollapsingToolbarLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 首页 Fragment
 */
public final class HomeFragment4 extends TitleBarFragment<HomeActivity>
        implements TabAdapter.OnTabListener, ViewPager.OnPageChangeListener,
        XCollapsingToolbarLayout.OnScrimsListener {

    public static HomeFragment4 newInstance() {
        return new HomeFragment4();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.home_fragment_4;
    }

    /**
     * 等待对话框
     */
    private BaseDialog mWaitDialog;

    @Override
    protected void initView() {
        if (mWaitDialog == null) {
            mWaitDialog = new WaitDialog.Builder(this.getAttachActivity())
                    // 消息文本可以不用填写
                    .setMessage("刷新中...")
                    .create();
        }
        setOnClickListener(R.id.my_4_btn_tab_total_status, R.id.my_4_btn_tab_light_status, R.id.my_4_btn_tab_tv_status,
                R.id.my_4_btn_switch_light_1, R.id.my_4_btn_switch_light_2, R.id.my_4_btn_switch_light_3, R.id.my_4_btn_switch_light_4
                , R.id.my_4_btn_switch_light_5, R.id.my_4_btn_switch_light_6, R.id.my_4_btn_switch_light_7, R.id.my_4_btn_switch_light_8,
                R.id.my_4_btn_switch_tv_1, R.id.my_4_btn_switch_tv_2, R.id.my_4_btn_switch_tv_3, R.id.my_4_btn_switch_tv_4
                , R.id.my_4_btn_switch_tv_5, R.id.my_4_btn_switch_tv_6, R.id.my_4_btn_switch_tv_7, R.id.my_4_btn_refresh);
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.my_4_btn_tab_total_status) {
            HomeActivity.mViewPager.setCurrentItem(0);
        } else if (viewId == R.id.my_4_btn_tab_light_status) {
            HomeActivity.mViewPager.setCurrentItem(1);
        } else if (viewId == R.id.my_4_btn_tab_tv_status) {
            HomeActivity.mViewPager.setCurrentItem(2);
        } else if (viewId == R.id.my_4_btn_refresh) {
            if (!mWaitDialog.isShowing()) {
                mWaitDialog.show();
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    MySocketClientManager.getInstance().reConnectAll();
                    EventBus.getDefault().post(new MessageEvent(4, 0, false));
                }
            }).start();
        }
    }

    @Override
    protected void onFragmentResume(boolean first) {
        super.onFragmentResume(first);

        new Thread(new Runnable() {
            @Override
            public void run() {
                EventBus.getDefault().post(new MessageEvent(4, 0, false));
            }
        }).start();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        // Do something
        if (event == null) return;
        if (event.getPage() != 4) return;
        int type = event.getType();
        boolean open = event.isOpen();
        Button button = null;
        try {
            if (type == 0) {
                button = findViewById(R.id.my_4_btn_switch_light_1);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_light_close : R.mipmap.my_light_open));

                button = findViewById(R.id.my_4_btn_switch_light_2);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_light_close : R.mipmap.my_light_open));

                button = findViewById(R.id.my_4_btn_switch_light_3);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_light_close : R.mipmap.my_light_open));

                button = findViewById(R.id.my_4_btn_switch_light_4);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_light_close : R.mipmap.my_light_open));

                button = findViewById(R.id.my_4_btn_switch_light_5);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_light_close : R.mipmap.my_light_open));

                button = findViewById(R.id.my_4_btn_switch_light_6);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_light_close : R.mipmap.my_light_open));

                button = findViewById(R.id.my_4_btn_switch_light_7);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_light_close : R.mipmap.my_light_open));

                button = findViewById(R.id.my_4_btn_switch_light_8);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_light_close : R.mipmap.my_light_open));

                // ========tv
                button = findViewById(R.id.my_4_btn_switch_tv_1);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_tv_close : R.mipmap.my_tv_open));
                button = findViewById(R.id.my_4_btn_switch_tv_2);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_tv_close : R.mipmap.my_tv_open));
                button = findViewById(R.id.my_4_btn_switch_tv_3);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_tv_close : R.mipmap.my_tv_open));
                button = findViewById(R.id.my_4_btn_switch_tv_4);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_tv_close : R.mipmap.my_tv_open));
                button = findViewById(R.id.my_4_btn_switch_tv_5);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_tv_close : R.mipmap.my_tv_open));
                button = findViewById(R.id.my_4_btn_switch_tv_6);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_tv_close : R.mipmap.my_tv_open));
                button = findViewById(R.id.my_4_btn_switch_tv_7);
                open = MySocketClientManager.getInstance().isConnected(button.getTag() + "");
                button.setBackground(ContextCompat.getDrawable(getContext(), !open ? R.mipmap.my_tv_close : R.mipmap.my_tv_open));
            }
        } catch (Exception e) {
            HomeFragment1.showError(getContext(), e.getMessage());
        } finally {
            if (mWaitDialog != null) {
                mWaitDialog.dismiss();
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public boolean isStatusBarEnabled() {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled();
    }

    /**
     * {@link TabAdapter.OnTabListener}
     */

    @Override
    public boolean onTabSelected(RecyclerView recyclerView, int position) {
        return true;
    }

    /**
     * {@link ViewPager.OnPageChangeListener}
     */

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    /**
     * CollapsingToolbarLayout 渐变回调
     * <p>
     * {@link XCollapsingToolbarLayout.OnScrimsListener}
     */
    @SuppressLint("RestrictedApi")
    @Override
    public void onScrimsStateChange(XCollapsingToolbarLayout layout, boolean shown) {
        getStatusBarConfig().statusBarDarkFont(shown).init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}